/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class Leftover {
    
    private ZooKeeper zk;
    private String thisAddress;
    private int thisPort;
    
     public void join(String groupName, String memberName)
            throws KeeperException, InterruptedException {

        String path = "/" + groupName + "/" + memberName;
        String createdPath = zk.create(path,
                null /*data*/,
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);

        System.out.println("Created " + createdPath);

    }
     
       public void delete(String groupName)
            throws KeeperException, InterruptedException {

        String path = "/" + groupName;

        try {
            List<String> children = zk.getChildren(path, false);
            for (String child : children) {
                zk.delete(path + "/" + child, -1);
            }
            zk.delete(path, -1);
        } catch (KeeperException.NoNodeException e) {
            System.out.printf("Group %s does not exist \n", groupName);
        }
    }
    
    
}
